package com.ruoyi.framework.youshuda;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.zgl.youshuda.core.domain.dto.ModelTablePageDomain;
import com.zgl.youshuda.core.utils.CommonUtil;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 表格数据处理
 */
public class TableSupportInYsd
{
    /**
     * 当前记录起始索引
     */
    public static final String PAGE_NUM = "pageNum";

    /**
     * 每页显示记录数
     */
    public static final String PAGE_SIZE = "pageSize";

    /**
     * 排序列
     */
    public static final String ORDER_BY_COLUMN = "orderByColumn";

    /**
     * 排序的方向 "desc" 或者 "asc".
     */
    public static final String IS_ASC = "isAsc";

    /**
     * 分页参数合理化
     */
    public static final String REASONABLE = "reasonable";

    public static final String List = "List";
    public static final String Detail = "Detail";
    public static final String Add = "Add";
    public static final String Update = "Update";
    public static final String Delete = "Delete";

    public static final String Import = "Import";

    public static final String Export = "Export";
    public static final String ExportTemp = "ExportTemp";

    public static String getIsAsc(String isAsc)
    {
        String _isAsc = "desc";
        if (StringUtils.isNotEmpty(isAsc))
        {
            // 兼容前端排序类型
            if ("ascending".equals(isAsc))
            {
                _isAsc = "asc";
            }
            else if ("descending".equals(isAsc))
            {
                _isAsc = "desc";
            }
        }
        return _isAsc;
    }

    /**
     * 封装分页对象
     */
    public static ModelTablePageDomain getPageParams(Map<String,Object> params,Map<String, Map> genTableColumnConfig)
    {
        ModelTablePageDomain modelTablePageDomain=new ModelTablePageDomain();
        if(params==null){
            return modelTablePageDomain;
        }
        //页码
        modelTablePageDomain.setPageNum(Convert.toInt(params.get(PAGE_NUM), 1));
        params.remove(PAGE_NUM);
        //每页条数
        modelTablePageDomain.setPageSize(Convert.toInt(params.get(PAGE_SIZE), 10));
        params.remove(PAGE_SIZE);
        //排序
        modelTablePageDomain.setOrderByColumn(Convert.toStr(params.get(ORDER_BY_COLUMN), "create_time"));
        params.remove(ORDER_BY_COLUMN);
        //排序的方向 "desc" 或者 "asc".
        modelTablePageDomain.setIsAsc(Convert.toStr(getIsAsc(String.valueOf(params.get(IS_ASC))), "desc"));
        params.remove(IS_ASC);
        //当为子表存在时，移除对应关联字段
        params.remove("$LinkCol$");
        /** 规则类型  符合任一：OR    符合全部：AND **/
        String ruleType = "AND";
        //参数
        Map<String,Object> columnParams=new HashMap<>();
        //表单数据处理
        String superQuery = String.valueOf(params.get("$SuperQuery$"));
        params.remove("$SuperQuery$");
        if(StringUtils.isNotEmptyIncludeNull(superQuery)){
            /**
             * 高级查询时，数据处理
             */
            Map superQueryInfo = JSONObject.parseObject(superQuery, Map.class);
            //规则类型
            ruleType = String.valueOf(superQueryInfo.get("ruleType"));
            //获取配置规则中的数据
            List<Map> ruleConfigs = JSONArray.parseArray(JSONObject.toJSONString(superQueryInfo.get("ruleConfigs")), Map.class);
            for(Map rule : ruleConfigs){
                //字段名
                String columnName = StringUtils.toUnderScoreCase(String.valueOf(rule.get("column")));
                //设置查询值
                columnParams.put(columnName,rule.get("queryVal"));
                //修改查询规则
                genTableColumnConfig.get(columnName).put("queryType",rule.get("queryType"));
            }
        }else{
            /**
             * 普通数据处理
             */
            //特殊值处理
            List<String> intType = Arrays.asList(new String[]{"Integer", "Long"});
            List<String> defTimeType = Arrays.asList(new String[]{"updateTime", "createTime"});
            for(Map.Entry<String,Object> column:params.entrySet()){
                if(defTimeType.contains(column.getKey())) continue;
                Object value = column.getValue();
                //字段名
                String columnName = StringUtils.toUnderScoreCase(column.getKey());
                //值为空的不传递
                if(value == null || StringUtils.isEmptyIncludeNull(String.valueOf(value))){
                    if(genTableColumnConfig!=null && genTableColumnConfig.size()>0){
                        Map config = genTableColumnConfig.get(columnName);
                        if(config!=null && intType.contains(config.get("javaType"))){
                            value=0;
                        }else{
                            value="";
                        }
                    }else{
                        continue;
                    }
                }
                columnParams.put(columnName,value);
            }
        }
        modelTablePageDomain.setParams(columnParams);
        //规则类型
        modelTablePageDomain.setRuleType(ruleType);

        return modelTablePageDomain;
    }
}
